﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;


namespace Common
{

    public class Cls_TestPointList : BaseNode
    {
        public Cls_TestPointList(XmlNode Node)
            : base(Node)
        {
            if (Node == null)
            {
                throw new XmlException("Node is equal null");
            }
            _XmlNode = Node;
        }

        private List<Cls_TestPoint> _list = null;

        public List<Cls_TestPoint> TestPoints
        {
            get
            {
                // List<BaseNode> lst = new List<BaseNode>();
                Cls_TestPoint c = null;

                if (null != _list)
                    _list.Clear();
                else
                    _list = new List<Cls_TestPoint>();

                foreach (XmlNode x in _XmlNode.SelectNodes("TestPoint"))
                {
                    c = new Cls_TestPoint(x);
                    //  BaseNode b = c as BaseNode;
                    _list.Add(c);
                }
                //lst.Sort(delegate(BaseNode x, BaseNode y) { return x.id.CompareTo(y.id); });
                //foreach (BaseNode b in lst)
                //{
                //    _list.Add(b as Cls_Channel);
                //}
                _list.Sort();
                return _list;
            }
        }

        public Cls_TestPoint TestPoint(int id)
        {
            XmlNode ch;
            XmlDocument doc = this._XmlNode.OwnerDocument;

            //add new task
            ch = _XmlNode.SelectSingleNode("TestPoint[@id=" + id.ToString() + "]");
            if (ch == null) return null;

            return new Cls_TestPoint(ch);
        }

        public void Sort()
        {

        }

        public Cls_TestPoint AddTestPoint(int id,string speed)
        {
            //find 
            XmlNode ch;
            XmlDocument doc = this._XmlNode.OwnerDocument;
            Cls_TestPoint ExistCh = TestPoint(id);
            if (null != ExistCh) return ExistCh;

            //add new task
            ch = _XmlNode.AppendChild(doc.CreateElement("TestPoint"));
            ch.Attributes.Append(doc.CreateAttribute("id")).Value = id.ToString();

            //set task property
            ch.AppendChild(doc.CreateElement("Name")).InnerText = "通道" + (id + 1);

            ch.AppendChild(doc.CreateElement("ChannelX")).InnerText ="-1";
            ch.AppendChild(doc.CreateElement("ChannelY")).InnerText = "-1";
   


            ch.AppendChild(doc.CreateElement("Description")).InnerText = speed;

            return new Cls_TestPoint(ch);
        }

        public void UpdateTestPoint(int id, string name, string data)
        {
            Cls_TestPoint ch = this.TestPoint(id);
            if (null == ch) return;
            XmlNode n = ch.Node.ParentNode;
            ch.Node.SelectSingleNode(name).InnerText = data;
        }

        public void DeleteTestPoint(int id)
        {
            Cls_TestPoint ch = this.TestPoint(id);
            if (null == ch) return;

            XmlNode n = ch.Node.ParentNode;
            n.RemoveChild(ch.Node);
        }
    }

    public class Cls_TestPoint : BaseNode
    {
        public Cls_TestPoint(XmlNode Node)
            : base(Node)
        {
            if (Node == null)
            {
                throw new XmlException("Node is equal null");
            }
            _XmlNode = Node;
        }

        public string ChannelX
        {
            get
            {
                if (_XmlNode == null || _XmlNode["ChannelX"].InnerText == "")
                {
                    return "";
                }
                return _XmlNode["ChannelX"].InnerText;
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["ChannelX"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("ChannelX"));
                _XmlNode["ChannelX"].InnerText = value.ToString();
            }
        }

        public string ChannelY
        {
            get
            {
                if (_XmlNode == null || _XmlNode["ChannelY"].InnerText == "")
                {
                    return "";
                }
                return _XmlNode["ChannelY"].InnerText;
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["ChannelY"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("ChannelY"));
                _XmlNode["ChannelY"].InnerText = value.ToString();
            }
        }

       



        public string Description
        {
            get
            {
                XmlNode x = _XmlNode["Description"];
                if (x != null)
                {
                    return x.InnerText;
                }
                else
                {
                    return "";
                }

            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["Description"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("Description"));
                _XmlNode["Description"].InnerText = value.ToString();
            }
        }




    }
}
